#include "log.h"

bool get_log_path(std::string &log_path)
{
    //log_path = "/var/log/dsa/logs/sniffer_db.log";
    //return true;
    
    char path[256] = {0};
    uint32_t i = 0;
    std::string file_path;
    
    if (readlink("/proc/self/exe", path, sizeof(path)) < 0) {
        return false;
    }

    i = (uint32_t)strlen(path);
    do {
        if (path[i - 1] == '/' || path[i - 1] == '\\') {
            path[i] = 0x0;
            break;
        }
    } while (i--);

    log_path.append(path);
    log_path.append(std::to_string(getpid()));
    log_path.append(".log");

    return true;
}

spdlogger_sptr g_logger = std::make_shared<Spdlogger>();

int log_init(spdlogger_sptr logger)
{
    std::string log_path;
    if (get_log_path(log_path) == false) {
        std::cout << "log init error! get log path failed!" << std::endl;
        return -1;
    }
    
    if (logger->init(log_path) == false) {
        std::cout << "log init error! init failed!" << std::endl;
        return -1;
    }
    MAIN_LOG_INIT(logger);
    MAIN_LOG_SETLEVEL(level::logger_level::trace, true);

    return 0;
}

void log_set_level(level::logger_level level, bool trace_func)
{
    MAIN_LOG_SETLEVEL(level, trace_func);
}